package com.system.fkjslee.hos;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MakeAppointment extends Activity implements View.OnClickListener {

    private Button btnAppoint;
    private TextView tvAppointMethod;
    private TextView tvAppointState;
    private TextView tvExpertDetail;
    private String appointMethod;
    private String expertDetail;
    private String appointState = "未预约";
    private String expertID;
    private Handler handler;
    private String returnData;
    private Activity activity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_make_appointment);
        activity = this;

        initView();
    }

    @SuppressLint("HandlerLeak")
    private void initView() {
        appointMethod = getIntent().getStringExtra("appointMethod");
        expertDetail = getIntent().getStringExtra("expertDetail");
        expertID = getIntent().getStringExtra("expertID");

        btnAppoint = findViewById(R.id.btnAppoint);
        tvAppointMethod = findViewById(R.id.tvAppointMethod);
        tvAppointState = findViewById(R.id.tvAppointState);
        tvExpertDetail = findViewById(R.id.tvExpertDetail);

        btnAppoint.setOnClickListener(this);

        tvAppointMethod.setText(appointMethod);
        tvAppointState.setText(appointState);
        tvExpertDetail.setText(expertDetail);

        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case 1:
                        switch (returnData) {
                            case "maxPerson":
                                Toast.makeText(activity, "已满", Toast.LENGTH_SHORT).show();
                                break;
                            case "has already":
                                Toast.makeText(activity, "你已经预约过了", Toast.LENGTH_SHORT).show();
                                break;
                            case "succeed":
                                Toast.makeText(activity, "成功预约", Toast.LENGTH_SHORT).show();
                                break;
                        }
                        break;
                }
            }
        };

    }

    public void onClick(View v) {
        switch(v.getId()) {
            case R.id.btnAppoint:
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Looper.prepare();
                        Message message = new Message();
                        String data = "patientID=" + LogActivity.account + "&expertID=" + expertID;
                        HttpThread httpThread = new HttpThread(LogActivity.url + "/appoint", data);
                        httpThread.run();
                        returnData = httpThread.getResult();
                        message.what = 1;
                        handler.sendMessage(message);
                        Looper.loop();
                    }
                }).start();
                break;
        }
    }
}
